package com.ssm.blog.controller;

import com.ssm.blog.pojo.User;
import com.ssm.blog.service.UserService;
import com.ssm.blog.util.Constants;
import com.sun.imageio.plugins.common.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //使用qq第三方登录
    @RequestMapping("/login.html")
    @ResponseBody
    public Map<String, Object> login(User user, HttpSession session) {
        User checkUser = userService.getUserByUserName(user.getUserName());
        if (checkUser == null) {//如果openId不存在就注册
            userService.addUser(user);
            session.setAttribute(Constants.SESSION_USER, user);
        } else {//如果openId存在就登录
            //update一下
            session.setAttribute(Constants.SESSION_USER, checkUser);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("status", true);
        return result;
    }

    @RequestMapping("/logout.html")
    @ResponseBody
    public Map<String, Object> logout(HttpSession session) {
        session.setAttribute(Constants.SESSION_USER, null);
        Map<String, Object> result = new HashMap<>();
        result.put("status", true);
        return result;
    }

}
